using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MemberuserController : ControllerBase
    {
        /// <summary>
        /// 会员表查看
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            var bll = new BLL.Memberuser();
            var model = bll.getById(id);
            if (model == null)
            {
                model = new EF.Models.Memberuser();
                //model.Mobile = "100***";
            }
            return apiJson.success(model);
        }

        /// <summary>
        /// 会员表删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public ActionResult<string> Delete(int id)
        {
            var bll = new BLL.Memberuser();
            var model = bll.getById(id);
            if (model == null) return apiJson.error("对象不存在");
            else
            {
                bll.Delete(id);
                return apiJson.success(model);
            }
        }

        /// <summary>
        /// 会员表更新
        /// formData为参数传入json条件串
        /// </summary>
        /// <returns></returns>
		[HttpPost]
        public ActionResult<string> Update()
        {
            string formData = Request.Form["formData"];
            var model = JsonConvert.DeserializeObject<EF.Models.Memberuser>(formData);
            //新增记录的默认参数
            if (model.Id <= 0)
            {
                //model.IsSystem = 999;
            }

            var bll = new BLL.Memberuser();
            var rows = bll.Update(model);
            if (rows > 0) return apiJson.success(model);
            else return apiJson.error("失败");
        }

        /// <summary>
        /// 会员表列表
        /// formData为参数传入json条件串
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<string> Index()
        {
            var formData = Request.Form["formData"];
            var dic = JsonConvert.DeserializeObject<Dictionary<string,string>>(formData);
            //此处可加入条件
            //dic["adminId"] = "323";

            var bll = new BLL.Memberuser();
            var pager= bll.List(dic);
            if (pager.totalRecords > 0) return apiJson.success(pager);
            else return apiJson.error("没有数据");
        }
    }
}

